﻿using Device.Views;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using static Device.DeviceModule;

namespace Device.ViewModels
{
    public class DeviceViewModel : BindableBase ,INavigationAware
    {
        private string _deviceName;
        private readonly IDialogService dialogService;

        public string DeviceName { get =>_deviceName; private set { 
            
                _deviceName = value;
                RaisePropertyChanged();
            } }

       

        public ICommand ShutdownMechine { get; private set; }

        public DeviceViewModel(IDialogService dialogService)
        {
            ShutdownMechine = new DelegateCommand<object>(ShutdownMechaine);
            this.dialogService = dialogService;
        }

        private void ShutdownMechaine(object obj) {
            /*
             * 传统的 每次使用 Dialog 都新建一个对象
             * 高耦合
            new DeviceDialog().ShowDialog();
            */

            DialogParameters dialogParameters = new DialogParameters();

            dialogParameters.Add("deviceName", DeviceName);

            // 使用对话框窗口的名称来调用这个对话框
            dialogService.ShowDialog(DeviceDialogName, dialogParameters, result => {

                if (result.Result == ButtonResult.OK) {

                    if(result.Parameters.TryGetValue("time",out DateTime time)){ 
                        MessageBox.Show($"{time}  关机成功");
                    }

                }

            });

        }


        /// <summary>
        /// 
        /// 多次进入本 view 时，是否重用本对象
        /// </summary>
        /// <param name="navigationContext"></param>
        /// <returns></returns>
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            if(navigationContext.Parameters.TryGetValue("deviceName", out string deviceName)){

                DeviceName = deviceName;
            }
        }
    }
}
